package cn.edu.nuaa.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.itextpdf.text.Paragraph;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

public class PdfUtils {
    // 单文件转换
    public static void convert(String inputDocxPath, String outputPdfPath) {
        try {
            // 读取Word文档
            FileInputStream fis = new FileInputStream(inputDocxPath);
            XWPFDocument document = new XWPFDocument(fis);

            // 创建PDF文档
            Document pdfDocument = new Document();
            PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(outputPdfPath));
            pdfDocument.open();

            // 将Word内容写入PDF
            PdfWriter.getInstance(pdfDocument, new FileOutputStream(outputPdfPath));
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                String text = paragraph.getText();
                pdfDocument.add(new Paragraph(text));
            }

            // 关闭文档
            writer.close();
            pdfDocument.close();
            document.close();

            System.out.println("Convert succeeded!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Convert failed!");
        }
    }

    //批量转换
    public static void convert(String inputDocxPath) {
        try {
            File folder = new File(inputDocxPath);
            File[] files = folder.listFiles();
            String path = folder.getAbsolutePath() + "/pdf";
            try {
                File file = new File(path);
                if (file.exists()) {
                    System.err.println("pdf目录已存在");
                    System.exit(-1);
                }
                file.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
            for (File file : files) {
                if (file.isFile()) {
                    String fileType = file.getName().substring(file.getName().lastIndexOf(".") + 1);
                    String fileName = path + "/" + file.getName().replaceFirst("[.][^.]+$", "") + ".pdf";
                    if (fileType.equals("doc") || fileType.equals("docx")) {
                        try {
                            // 读取Word文档
                            FileInputStream fis = new FileInputStream(file);
                            XWPFDocument document = new XWPFDocument(fis);
                            // 创建PDF文档
                            Document pdfDocument = new Document();
                            PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(fileName));
                            pdfDocument.open();

                            // 将Word内容写入PDF
                            PdfWriter.getInstance(pdfDocument, new FileOutputStream(fileName));
                            for (XWPFParagraph paragraph : document.getParagraphs()) {
                                String text = paragraph.getText();
                                pdfDocument.add(new Paragraph(text));
                            }

                            // 关闭文档
                            writer.close();
                            pdfDocument.close();
                            document.close();

                            System.out.println("Convert succeeded!");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Convert failed!");
        }
    }
}
